package com.lihao.sam.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lihao.sam.dao.StatisticsDao;
import com.lihao.sam.dto.ConstructionDegreeDto;
import com.lihao.sam.entity.Statistics;
import com.lihao.sam.service.intf.StatisticsService;
import com.lihao.sam.vo.PageVo;
import com.lihao.sam.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author Qinheyun
 * @Description:TODO
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private StatisticsDao dao;
    @Override
    public R add(Statistics statistics) {
        if (dao.add(statistics)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R update(Statistics statistics) {
        if (dao.update(statistics)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public R del(int id) {
        if (dao.del(id)>0){
            return R.ok();
        }else {
            return R.fail();
        }
    }

    @Override
    public PageVo all(String campus, String storey, int page, int limit) {
        PageHelper.startPage(page,limit);
        PageInfo<Statistics> pageInfo = new PageInfo<>(dao.all(campus, storey));
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public R importData(MultipartFile file) throws IOException {
        //实现Excel文件导入  1.文件上传  2.解析Excel 3.操作数据库
        //验证文件是否为空
        if (!file.isEmpty()){
            //2.获取文件内容  流
            InputStream is = file.getInputStream();
            //3.解析Excel文件  文件不是一条数据 使用集合
            List<Statistics> list= EasyExcel.read(is, Statistics.class, new ReadListener() {
                @Override
                public void invoke(Object o, AnalysisContext analysisContext) {
                }
                @Override
                public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                }
            }).sheet().doReadSync();
            //4.mybatis实现批量新增
            if (dao.addBatch(list)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public void exportData(HttpServletResponse response) throws IOException {
        //1.设置下载的文件名
        String fn="grade_"+System.currentTimeMillis()+".xlsx";
        //2.设置响应消息头 实现文件下载
        response.setHeader("content-disposition","attachment;filename="+fn);
        //3.写出要下载的内容
        EasyExcel.write(response.getOutputStream(), Statistics.class).
                sheet().doWrite(dao.selectAll());
    }

    @Override
    public PageVo quer(int page, int limit) {
        PageHelper.startPage(page,limit);
        PageInfo<ConstructionDegreeDto> pageInfo = new PageInfo<>(dao.quer());
        return PageVo.ok(pageInfo.getTotal(),pageInfo.getList());
    }
}
